package org.jeecg.common.util.push;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.DeviceType;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @author guoyh
 * @ClassName JPushUtils
 * @Description
 * @since 2020/10/12 23:57
 **/
@Slf4j
public class JPushUtils {


    private final JPushClient jPushClient;

    public JPushUtils(String appKey,String masterSecret){
        this.jPushClient = new JPushClient(masterSecret,appKey,null,ClientConfig.getInstance());
    }

    public void sendRequest(PushPayload payload){
        try {
            PushResult result = jPushClient.sendPush(payload);
            int status = result.getResponseCode();
            log.info("Got result - " + result);
        } catch (APIConnectionException e) {
            log.error("Connection error. Should retry later. ", e);
            log.error("Sendno: " + payload.getSendno());
        } catch (APIRequestException e) {
            log.error("Error response from JPush server. Should review and fix it. ", e);
            log.info("HTTP Status: " + e.getStatus());
            log.info("Error Code: " + e.getErrorCode());
            log.info("Error Message: " + e.getErrorMessage());
            log.info("Msg ID: " + e.getMsgId());
            log.error("Sendno: " + payload.getSendno());
        }
    }

    public PushPayload buildPayload(List<String> registrationId,DeviceType deviceType,String alert,String title,Map<String,String> extras){
        Platform platform = Platform.newBuilder().addDeviceType(deviceType).build();
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(platform)
                .setAudience(Audience.registrationId(registrationId))
                .setNotification(buildNotification(deviceType,alert,title,extras))
                .build();
        return payload;
    }

    private Notification buildNotification(DeviceType deviceType, String alert, String title, Map<String,String> extras){

        if (deviceType.equals(DeviceType.Android)){
            return Notification.android(alert,title,extras);
        }else if (deviceType.equals(DeviceType.IOS)){
            return Notification.ios(alert,extras);
        }else{
            throw new RuntimeException("未知的平台类型");
        }
    }

    public static void main(String[] args) {
        JPushUtils jPushUtils = new JPushUtils("fb349b3e1d7fba842fdcac0a","3009fd29ef7129c375c3b809");
        Map<String,String> map = Maps.newHashMap();
        map.put("type","chat");
        map.put("chatId","1011");
        jPushUtils.sendRequest(jPushUtils.buildPayload(Lists.newArrayList("170976fa8a3405dc90b"),DeviceType.Android,"大夫我还有救吗","您有一条新消息",map));
    }


}
